/* @(#)GSManufactureScanQueryAction.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-13    新增
 *   Jenny Li     2006-3-4   将读取产品编码改成读取集中编码
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import system.LoginContext;

import com.apep.gw.service.StuationAnalyse;
import com.apep.gw.service.impl.ProductStuationAnalyse;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.form.ManufactureSetForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DateUtil;
import framework.core.SrvContext;

/**
 * 产品设置操作
 */
public class GSManufactureSetAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
        String tOptype = (String) aForm.getRequest().getParameter("optype");
        String tParam = (String) aForm.getRequest().getParameter("param");
        ManufactureSetForm mf=(ManufactureSetForm) aForm;
        String[] tManufactures = mf.getSel_manufacture();

        
        /*
         * 潘丞寅 09.11.5 增加 修改Last_update_user_id等数据
         */
        SrvContext context = SrvContext.getContext(aForm.getRequest());
        LoginContext loginInfo = LoginContext.get(context);
        String Last_update_time=new DateUtil().getFullString();
        String Last_update_user_id=loginInfo.getLoginUserId();
        String Last_update_user=loginInfo.getLoginUserName(); 
        StuationAnalyse productStuationAnalyse = new ProductStuationAnalyse();
 
        GSManufactureProcessor processor = new GSManufactureProcessor();
        GSManufactureDAO tGSManufactureDAO = new GSManufactureDAO();
        if ("1".equals(tOptype)) {
            // 设置产品的结算模式
            for (int i = 0; i < tManufactures.length; i++) {
                if (tManufactures[i] != null && !tManufactures[i].equals("")) {
                    String[] tManuData = tManufactures[i].split("%");
                    GSManufactureBean mbean = processor.getManufInfoByConcentricityId(tManuData[6].trim());
                    if (mbean == null) {
                           throw new GSException("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                                  + "集中编码 " + tManuData[6].trim()
                                   + " 不存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能修改！");
                    }
                    if (tGSManufactureDAO.modifyGs_balance_mode(tParam,Last_update_time ,Last_update_user_id,Last_update_user,tManuData[0]) == false) {
                        throw new GSException("设置产品结算模式失败! 当前产品编码：" + tManuData[0]);
                    }
            		
                    GSManufactureVO vo = new GSManufactureVO();
                    vo.setGs_balance_mode(tParam);
                    vo.setIs_buy(mbean.getIs_buy());
                    if(!"".equals(processor.appendUpdateContent(mbean,vo))){
                    	  productStuationAnalyse.update(processor.appendUpdateContent(mbean,vo), new DateUtil().getFullString(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean.getId());
                    }             
                    //tGSManufProcessor.freshFilterByManufactorID(tManuData[0]);
                }
            }

        } else if ("2".equals(tOptype)) {
            // 设置产品采购属性
        	
        	
        	 GSProductBidDao bidDao = new GSProductBidDao();
        	 GSProductTypeDao dao = new GSProductTypeDao();
            for (int i = 0; i < tManufactures.length; i++) {
                if (tManufactures[i] != null && !tManufactures[i].equals("")) {
                    String[] tManuData = tManufactures[i].split("%");
                    GSManufactureBean mbean = processor.getManufInfoByConcentricityId(tManuData[6].trim());
                    if (mbean == null) {
                           throw new GSException("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                                  + "集中编码 " + tManuData[6].trim()
                                   + " 不存在，<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不能修改！");
                    }
                    if (tGSManufactureDAO.modifyManufactureIsBuy(tParam,Last_update_time ,Last_update_user_id,Last_update_user,tManuData[0]) == false) {
                        throw new GSException("设置产品采购属性信息失败!  当前产品编码：" + tManuData[0]);
                    }else{
                    	if(tParam.equals("0")){
	                      //设置采购属性为0时 修改对应的产品使用分类和产品招标分类
                    		
	                  	  GSProductTypeBean bean = dao.findProductTypeByProuductCode(tManuData[6].trim());
	                      if(bean !=null){
	                    	  dao.updatePrudctTypeStatusByProductCode(tManuData[6].trim() );
	                      }
	                     
	                      GSProductBidBean bidBean = bidDao.findProductBidByProuductCode(tManuData[6].trim());
	                      if(bidBean !=null){
	                    	  bidDao.updatePrudctBidStatusByProductCode(tManuData[6].trim());
	                      }
	                      GSManufactureVO vo = new GSManufactureVO();
	                      vo.setIs_buy(mbean.getIs_buy());
	                      productStuationAnalyse.close(processor.appendUpdateIsBuy(mbean,vo), new DateUtil().getFullString(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean.getId());
                    	}else if(tParam.equals("1")){
                        	//设置采购属性为1时 修改对应的产品使用分类和产品招标分类
                             GSManufactureVO vo = new GSManufactureVO();
   	                      	 vo.setIs_buy(tParam);
   	                      	 if(mbean.getIs_buy().equals("0")){
   	                      		 productStuationAnalyse.open(processor.appendUpdateIsBuy(mbean, vo), new DateUtil().getFullString(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(),mbean!=null?mbean.getId():"");
   	                      	 }
   	                      	 if(!"".equals(processor.appendUpdateContent(mbean,vo))){
   	                     	 productStuationAnalyse.update(processor.appendUpdateContent(mbean,vo), new DateUtil().getFullString(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean.getId()); 
   	                      	 }
   	                  	                     
    	                  	  GSProductTypeBean bean = dao.findProductTypeByProuductCodeNoStauts(tManuData[6].trim());
    	                      if(bean !=null){
    	                    	  dao.updatePrudctTypeIsStatusByProductCode(mbean.getGs_concentricity().trim());
    	                      }
    	                      GSProductBidBean bidBean = bidDao.findProductBidByProuductCodeNoStauts(mbean.getGs_concentricity().trim());
    	                      if(bidBean !=null&&bean!=null&&String.valueOf(bean.getProductType()).equals(GSBaseConstants.PRODUCT_TYPE1)){
    	                    	  bidDao.updatePrudctIsBidStatusAndTypeAndIsBidByProductCode(mbean.getGs_concentricity());
    	                      }
                          }
                    }
                    //tGSManufProcessor.freshFilterByManufactorID(tManuData[0]);
                }
            }

        } else if ("3".equals(tOptype)) {
            // 设置产品的有效性
            for (int i = 0; i < tManufactures.length; i++) {
                if (tManufactures[i] != null && !tManufactures[i].equals("")) {
                    String[] tManuData = tManufactures[i].split("%");
                    if (tGSManufactureDAO.modifyGs_validity(tParam, tManuData[0]) == false) {
                        throw new GSException("设置产品有效性失败!  当前产品编码：" + tManuData[0]);
                    }
                    //tGSManufProcessor.freshFilterByManufactorID(tManuData[0]);
                }
            }
        }

        aForm.getRequest().setAttribute("requery", "true");

        // 查询条件
        aForm.getRequest().setAttribute("s_operOrgId", aForm.getRequest().getParameter("s_operOrgId"));
        aForm.getRequest().setAttribute("s_operId", aForm.getRequest().getParameter("s_operId"));
        aForm.getRequest().setAttribute("s_operName", aForm.getRequest().getParameter("s_operName"));
        aForm.getRequest().setAttribute("s_keyWord", aForm.getRequest().getParameter("s_keyWord"));

        aForm.getRequest().setAttribute("iskeywordquery", aForm.getRequest().getParameter("iskeywordquery"));
        aForm.getRequest().setAttribute("message", "产品信息设置完成！");
        aForm.getRequest().setAttribute("backstep", "-1");

        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
